﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ebay.Dal.CenterDB_SystemTableAdapters;

namespace ebay.Dal
{
    public class Users
    {
        public void SyncUsersFromK3(string Muser)
        {
            SqlParameter p = new SqlParameter("@Muser", SqlDbType.VarChar);
            p.Value = Muser;
            SqlHelper.ExecuteNonQuery(SqlHelper.ceterDB_Conn, CommandType.StoredProcedure, "sp_syncUserFromK3", p);
        }

        public bool DeleteUser(string UserId)
        {
            using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
            {
                connection.Open();
                T_UsersTableAdapter uadapter = new T_UsersTableAdapter();
                T_UserRightTableAdapter radapter = new T_UserRightTableAdapter();
                uadapter.Connection = connection;
                radapter.Connection = connection;
                using (SqlTransaction trans = connection.BeginTransaction())
                {
                    uadapter.Transaction = trans;
                    radapter.Transaction = trans;
                    try
                    {
                        uadapter.Delete(UserId);
                        radapter.DeleteByUserId(UserId);
                        trans.Commit();
                        return true;
                    }
                    catch {
                        trans.Rollback();
                        return false;
                    }
                }
            }
        }
    }
}
